package easy

import "sort"

func MaxmiumScore(cards []int, cnt int) int {
	sort.Ints(cards)
	len := len(cards)
	if len < cnt {
		return 0
	}
	sum := 0
	minOdd := 1001
	minEven := 1001
	for i := len - 1; i >= len-cnt; i-- {
		cur := cards[i]
		if cur%2 == 1 {
			minOdd = min(minOdd, cur)
		} else {
			minEven = min(minEven, cur)
		}
		sum += cur
	}
	if sum%2 == 0 {
		return sum
	} else {
		maxOdd, maxEven := -1, -1
		for i := len - cnt - 1; i >= 0; i-- {
			cur := cards[i]
			if cur%2 == 1 {
				maxOdd = max(maxOdd, cur)
			} else {
				maxEven = max(maxEven, cur)
			}
		}
		var ans1, ans2 int
		if maxEven != -1 && minOdd != 1001 {
			ans1 = sum - minOdd + maxEven
		}
		if maxOdd != -1 && minEven != 1001 {
			ans2 = sum - minEven + maxOdd
		}
		return max(ans1, ans2)
	}
}
